/*  ------------------------------------------------------------------------
	 NQ Common Services Library
	 
	 Homepage: http://www.awzhome.de/
	 ------------------------------------------------------------------------
	 
	 The contents of this file are subject to the Mozilla Public License
	 Version 1.1 (the "License"); you may not use this file except in
	 compliance with the License. You may obtain a copy of the License at
	 http://www.mozilla.org/MPL/
	 
	 Software distributed under the License is distributed on an "AS IS"
	 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	 License for the specific language governing rights and limitations
	 under the License.
	
	 The Original Code is code of NQ Common Services Library.

	 The Initial Developer of the Original Code is Andreas Weizel.
	 Portions created by the Initial Developer are
	 Copyright (C) 2006-2009 Andreas Weizel. All Rights Reserved.
	 
	 Contributor(s): (none)
	 
	 Alternatively, the contents of this file may be used under the terms
	 of the GNU General Public License Version 2.0 or later (the "GPL"),
	 or the GNU Lesser General Public License Version 2.1 or later (the
	 "LGPL"), in which case the provisions of the GPL or the LGPL are
	 applicable instead of those above. If you wish to allow use of your
	 version of this file only under the terms of the GPL or the LGPL and
	 not to allow others to use your version of this file under the MPL,
	 indicate your decision by deleting the provisions above and replace
	 them with the notice and other provisions required by the GPL or the
	 LGPL. If you do not delete the provisions above, a recipient may use
	 your version of this file under the terms of any of the MPL, the GPL
	 or the LGPL.
	 ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Text;
using AWZhome.NQ.Core;
using AWZhome.NQ.Core.Fluent;

namespace AWZhome.NQ.CommonServices
{
	/// <summary>
	/// Helper class which encapsulates much of the initialization code needed
	/// for session services.
	/// </summary>
	/// <remarks>
	/// You don't need to use this class to create a session service, you may also directly implement
	/// <see cref="AWZhome.NQ.CommonServices.ISession">INQSessionService</see>
	/// interface instead. But this class could reduce the amount of needed code.
	/// </remarks>
	public abstract class SessionServiceBase : ISession
	{
		private string context = null;
		private string sessionName = null;
		private bool isOpen = false;

		#region INQSessionService Members

		public event EventHandler SessionOpened;
		public event EventHandler SessionClosed;

		public void OpenSession()
		{
			OpenSession("");
		}

		public void OpenSession(string context)
		{
			if (!isOpen)
			{
				this.context = context;

				// Generate unique session name
				ISessionManager sessionManager =
					ServiceCore.Get<Services.ISessionManagerService>().To;
				sessionName = sessionManager.GenerateSessionName(this);

				// Call child implementation
				OpenSessionInternal();

				// Raise SessionOpened event
				OnSessionOpened();
				isOpen = true;
			}
			else
			{
				throw new InvalidOperationException("This session is already open.");
			}
		}

		public void CloseSession()
		{
			if (isOpen)
			{
				// Call child implementation
				CloseSessionInternal();

				// Raise SessionClosed event
				OnSessionClosed();
			}
			else
			{
				throw new InvalidOperationException("This session is closed.");
			}
		}

		/// <summary>
		/// Method called when the session is opened.
		/// </summary>
		/// <remarks>
		/// As <c>NQSessionServiceBase</c> overrides the original 
		/// <see cref="AWZhome.NQ.CommonService.INQSessionService.OpenSession">OpenSession</see>
		/// method, it calls <c>OpenSessionInternal</c> to give the service the chance to
		/// execute it's own code during the session's initialization process.
		/// </remarks>
		protected virtual void OpenSessionInternal()
		{
		}

		/// <summary>
		/// Method called when the session is closed.
		/// </summary>
		/// <remarks>
		/// As <c>NQSessionServiceBase</c> overrides the original 
		/// <see cref="AWZhome.NQ.CommonService.INQSessionService.CloseSession">CloseSession</see>
		/// method, it calls <c>CloseSessionInternal</c> to give the service the chance to
		/// execute it's own code during the session's destruction process.
		/// </remarks>
		protected virtual void CloseSessionInternal()
		{
		}

		public abstract string Protocol
		{
			get;
		}

		public abstract string DisplayName
		{
			get;
		}

		public string SessionName
		{
			get
			{
				return sessionName;
			}
		}

		public string Context
		{
			get
			{
				return context;
			}
		}

		public bool IsOpen
		{
			get
			{
				return isOpen;
			}
		}

		#endregion

		#region Private/protected methods

		/// <summary>
		/// Raises the 
		/// <see cref="AWZhome.NQ.CommonServices.ISession.SessionOpened">SessionOpened</see>
		/// event.
		/// </summary>
		protected void OnSessionOpened()
		{
			if (this.SessionOpened != null)
			{
				this.SessionOpened(this, new EventArgs());
			}
		}

		/// <summary>
		/// Raises the 
		/// <see cref="AWZhome.NQ.CommonServices.ISession.SessionClosed">SessionClosed</see>
		/// event.
		/// </summary>
		protected void OnSessionClosed()
		{
			if (this.SessionClosed != null)
			{
				this.SessionClosed(this, new EventArgs());
			}
		}

		#endregion
	}
}
